<?php

namespace app\api\model;

use think\Model;

class Login_M extends Model
{
    protected $table = 't_av_room';
    
    public function login($date)
    {   

        if($date['name'] == 'textteacher'){
            $res['code'] = 3;
            $info = [
                'id' => '38',  // 房间号
                'token' => 'dGV4dFRlYWNoZXIxNDk5MDcxMTI0',
                'userType' => 0,
                'user_sig' => 'eJxlj1FvgjAUhd-5FQ2vLksLZYCJD7hhskwYbKDbE*lohYpigQrqsv*uYyaS7PHm*3LOPd8KAECN5u-3JE13*1Im8iiYCsZAherdDQrBaUJkotf0H2QHwWuWkJVkdQ81w9YgHCqcslLyFb8Kkh1kxEiaX*6b1NAi6Yt6B*FLhI4xehgqPOuh54aPz7P0aHHfgx9mPt36Xr7Wq2g2f3Opoy*fcPdlRe4pmPqfZLR2uIODqBWdkVVW9ho3-nJjslHsVF28KHTS4n3hLcJg85LmNJxMBpWSb9n1IduGJkIaHtCW1Q3flX*zITIQQjb83a78KGeen19K',
            ];
            $res['info'] = $info;
            return $res; 
        }
        $time = time();
        $info = $this
                ->alias('a')
                ->where('a.teacher_pass',$date['pwd'])
                ->where('a.uid',$date['name'])
                ->where('a.start_time < '.$time.' AND '.$time.' < a.end_time')
                ->field('a.id,a.teacher_pass,a.student_pass')
                ->find();
        if($num = Db('account')->where('uid',$date['name'])->count()){
            $res['code'] = 1;
            return $res;
        }
        
        $cmd = $data['uid'] . time();
        $data = [
            'uid'           => $date['name'],
            'pwd'           => $date['pwd'],
            'user_sig'      => $date['user_sig'],
            'token'         => base64_encode($cmd),
            'state'         => 1,
            'register_time' => time(),
            'login_time'    => time(),
        ];
        
        if($info){
            
            $this->where('id',$info['id'])->update(['status'=>'1']); // 把房间状态更改为正在上课
            $data['userType'] = 0;//老师  
        }else{
            $info = $this
                    ->alias('a')
                    ->where('a.student_pass',$date['pwd'])
                    ->where('a.start_time < '.$time.' AND '.$time.' < a.end_time')
                    ->field('a.id,a.status')
                    ->find();
            if(!$info){
                $res['code'] = 0;
                return $res;
            }

            if($info['status'] == 0){
                $res['code'] = 4;
                return $res;
            }
            $resnum =  Db('class_detail')->where('class_id',$info['id'])->count();
            if($resnum >= 3){
                $res['code'] = 2;
                return $res;
            }
            $data['userType'] = 1;//学生
        }    

        $array = [
                    'uid'      => $data['uid'],
                    'com_time' => time(),
                    'IP'       => $_SERVER['REMOTE_ADDR'],
                    'class_id' => $info['id'],
                 ];
        Db('class_detail')->insert($array);
        Db('account')->insert($data);

        $info['token'] = $data['token']; 
        $info['userType'] = $data['userType']; 

        cache($data['token'], $data);
        $res['code'] = 3;
        $info = [
            'id' => $info['id'],  // 房间号
            'token' => $info['token'],
            'userType' => $info['userType'],
            'user_sig' => $date['user_sig'],
        ];
        $res['info'] = $info;
        return $res;
    }


    // 完整上线可用版。
    public function logind($date)
    {   
        $time = time();
        $info = $this
                ->alias('a')
                ->where('a.teacher_pass',$date['pwd'])
                ->where('a.uid',$date['name'])
                // ->where('a.start_time < '.$time.' AND '.$time.' < a.end_time')
                ->field('a.id,a.teacher_pass,a.student_pass')
                ->find();
        if($num = Db('account')->where('uid',$date['name'])->count()){
            $res['code'] = 1;
            return $res;
        }
        
        $cmd = $date['name'] . time();
        $data = [
            'uid'           => $date['name'],
            'pwd'           => $date['pwd'],
            'user_sig'      => $date['user_sig'],
            'token'         => base64_encode($cmd),
            'state'         => 1,
            'register_time' => time(),
            'login_time'    => time(),
        ];
        
        if($info){
            
            $this->where('id',$info['id'])->update(['status'=>'1']); // 把房间状态更改为正在上课
            $data['userType'] = 0;//老师  
        }else{
            $info = $this
                    ->alias('a')
                    ->where('a.student_pass',$date['pwd'])
                    ->where('a.start_time < '.$time.' AND '.$time.' < a.end_time')
                    ->field('a.id,a.status')
                    ->find();
            if(!$info){
                $res['code'] = 0;
                return $res;
            }

            if($info['status'] == 0){
                $res['code'] = 4;
                return $res;
            }
            $resnum =  Db('class_detail')->where('class_id',$info['id'])->count();
            if($resnum >= 3){
                $res['code'] = 2;
                return $res;
            }
            $data['userType'] = 1;//学生
        }    

        $array = [
                    'uid'      => $data['uid'],
                    'com_time' => time(),
                    'IP'       => $_SERVER['REMOTE_ADDR'],
                    'class_id' => $info['id'],
                 ];
        Db('class_detail')->insert($array);
        Db('account')->insert($data);

        $info['token'] = $data['token']; 
        $info['userType'] = $data['userType']; 

        cache($data['token'], $data);
        $res['code'] = 3;
        $info = [
            'id' => $info['id'],  // 房间号
            'token' => $info['token'],
            'userType' => $info['userType'],
            'user_sig' => $date['user_sig'],
        ];
        $res['info'] = $info;
        return $res;
    }
}